var utils = require('../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    needMsg: false,
    msgPhone: '',
    msgCode: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var info;
    if (app.globalData.personInfo) {
      info = app.globalData.personInfo;
      info.SEXVAL = info.USERSEX == '0' ? '男' : '女';
    }
    else {
      info = {
        USERNAME: '',
        USERSEX: '0',
        SEXVAL: '男',
        MOBILE: '',
        USERBIRTHDAY: ' 1990-01-01'
      }
    }
    var curdate = app.formatDateTime((new Date()).getTime() * 0.001, false);
    this.setData({
      info: info,
      endtime: curdate,
      sexArr: ['男', '女']
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindSexChange: function (e) {
    console.log(e.detail.value);
    var info = this.data.info;
    if (e.detail.value == 0) {
      info.USERSEX = 0;
      info.SEXVAL = '男';
    }
    else {
      info.USERSEX = 1;
      info.SEXVAL = '女';
    }
    this.setData({
      info: info
    });
  },
  bindDateChange: function (e) {
    console.log(e.detail.value);
    var info = this.data.info;
    info.USERBIRTHDAY = e.detail.value;
    this.setData({
      info: info
    });
  },
  checkInput: function (e) {
    var itype = e.currentTarget.dataset.inputtype;
    var info = this.data.info;
    if (itype == 'name') {
      info.USERNAME = e.detail.value;
    }
    else if (itype == 'phone') {
      info.MOBILE = e.detail.value;
      this.setData({
        needMsg: true
      });
    }
    this.setData({
      info: info
    });
  },
  submitInfo: function () {
    if (this.data.info.USERNAME == '' || this.data.info.MOBILE == '') {
      wx.showToast({
        title: '信息不完整',
        icon: 'none'
      });
      return;
    }
    if (!utils.isPhoneAvailable(this.data.info.MOBILE)){
      wx.showToast({
        title: '手机号码格式不正确',
        icon: 'none'
      });
      return;
    }
    if (this.data.needMsg){
      if (this.data.msgCode == ''){
        return;
      }
    }
    var url = '/yuanmai_we/usercenter/user/registerForVip';
    var params = {
      __zaofans: true,
      USERNAME: this.data.info.USERNAME,
      USERSEX: this.data.info.USERSEX,
      MOBILE: this.data.info.MOBILE,
      USERBIRTHDAY: this.data.info.USERBIRTHDAY
    }
    if (this.data.needMsg){
      console.log(this.data.msgCode);
      params.CHECKCODE = this.data.msgCode;
    }
    app.loadDataFromServer(url, this.submitCallBack, params, 'post', true);
  },
  submitCallBack: function (res) {
    console.log(res);
    if (res.success) {
      wx.showToast({
        title: '修改成功',
        duration: 1000
      });
      app.globalData.personInfo = {
        USERNAME: this.data.info.USERNAME,
        USERSEX: this.data.info.USERSEX,
        MOBILE: this.data.info.MOBILE,
        USERBIRTHDAY: this.data.info.USERBIRTHDAY
      };
      var delayid = setTimeout(function () {
        clearTimeout(delayid);
        wx.navigateBack({});
      }, 1000);
    }
    else {
      wx.showToast({
        title: res.error,
        icon: 'none'
      });
    }
  },
  msgPhoneInput: function (e) {
    this.setData({
      msgPhone: e.detail.value
    });
  },
  msgCodeInput: function (e) {
    this.setData({
      msgCode: e.detail.value
    });
  },
  sendMsgAction: function (e) {
    if (this.data.msgPhone == '') {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      });
      return;
    }
    if (!utils.isPhoneAvailable(this.data.msgPhone)) {
      wx.showToast({
        title: '手机号码格式不正确',
        icon: 'none'
      });
      return;
    }
    var url = '/yuanmai_we/activity/sms/sendCheckCode';
    var property = {
      "MOBILE": this.data.msgPhone,
      "ACTION": "updateMobile"
    };
    var params = {
      datas: JSON.stringify(property),
      __zaofans: true
    }
    app.loadDataFromServer(url, this.sendMsgActionCallBack, params, 'post', true);
  },
  sendMsgActionCallBack:function(res){
    console.log(res);
    if (res.success){
      wx.showToast({
        title: '发送成功',
      });
    }
    else{
      wx.showToast({
        title: res.errMsg,
        icon: 'none'
      });
    }
  }
})